<?php

namespace app\services;

use plugin\admin\app\model\ShareHolding;
use plugin\admin\app\model\UserIdentify;
use plugin\admin\app\model\UserProperty;

class ShareHoldingService
{
    public static function add($userId)
    {
        if (!self::get($userId)) {
            return ShareHolding::insertGetId([
                'user_id' => $userId,
                'no' => self::generateUniqueId(),
                'created_time' => date('Y-m-d')
            ]);
        }
    }

    public static function get($userId = null)
    {
        $userId = $userId ?? get_user_id();
        $data = ShareHolding::where('user_id', $userId)->first();

        if ($data) {
            $propertyInfo = UserProperty::where('user_id', $userId)->first();
            $identifyInfo = UserIdentify::where('user_id', $userId)->first();
            $data['name'] = $identifyInfo['name'];
            $data['idcard'] = hideIdCardNo($identifyInfo['idcard']);
            $data['company_name'] = ConfigService::get('company_name');
            $data['sd'] = $propertyInfo['sd'];
            return $data;
        }

        return null;
    }

    public static function generateUniqueId() {
        return bin2hex(random_bytes(8));
    }

}